#!/bin/bash

CWD=$(dirname "$0")
TEXT="$CWD/filetype-names.txt"
UNKOWN=true

if [ ! -n "$1" ] #check if a file is given
then
echo "You have to specifiy a file"
exit 1
fi

if [ ! -f "$1" ]  #check if it's a file
then
echo "Given file does not exist"
exit 1
fi

FMIME=$(file --mime-type -b "$1")

if [ -f "$TEXT" ]			#check if source file is given
then

	while read MIME #read line per line
	do
		if ([ "$(echo $MIME | cut -c1)" != "#" ] && [ -n "$MIME" ])  #skip comments
		then			

			if [[ $FMIME =~ .*$(echo $MIME | cut -d\  -f1).* ]]  #check if we have found a matching line
			then
				echo "type {$(echo $MIME | cut -d\  -f2- )}"	#return the value
				UNKOWN=false
				break
			fi
		fi

	done < $TEXT
	
	if [ $UNKOWN == true ]	#return ? on failure
	then
		echo "type ?"
	fi

fi
